package com.aqie.meet.ui;

import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;

import com.aqie.libcommon.adapter.CommonViewHolder;
import com.aqie.libcommon.app.BaseApplication;
import com.aqie.libfactory.model.api.RspModel;
import com.aqie.libfactory.model.api.user.UserPost;
import com.aqie.libfactory.net.Network;
import com.aqie.libfactory.net.RemoteService;
import com.aqie.meet.R;
import com.aqie.meet.ui.login.AccountActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class BusinessBaseFragment extends Fragment {
    protected final String TAG = getClass().getSimpleName();
    // 动态点赞
    protected void digPost(String postId){
        RemoteService service = Network.remote();
        Call<RspModel<Boolean>> call = service.postLike(postId);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {

                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getActivity());
                    return;
                }
                if(body !=null && body.success()){
                    // BaseApplication.showToast("点赞成功");
                }else {
                    Log.e(TAG, "request fail");
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {
                Log.e(TAG, t.getMessage());
            }
        });
    }

    // 取消动态点赞
    protected void unDigPost(String postId){
        RemoteService service = Network.remote();
        Call<RspModel<Boolean>> call = service.postUnlike(postId);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {
                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getActivity());
                    return;
                }
                if(body !=null && body.success()){
                    // BaseApplication.showToast("取消点赞成功");
                }else {
                    Log.e(TAG, "request fail");
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {
                Log.e(TAG, t.getMessage());
            }
        });
    }

    protected void digOrUndig(UserPost model, CommonViewHolder viewHolder) {
        // 点赞
        viewHolder.getView(R.id.like_num).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                digPost(model.getId());
                viewHolder.goneIfEmpty(R.id.like_num);
                viewHolder.show(R.id.liked_num);
                int digNum = model.getBoolLike() ? model.getLikeCount() : model.getLikeCount()+1;
                // 开始喜欢 点赞 不变， 开始不喜欢 点赞+1
                viewHolder.setText(R.id.liked_num, String.valueOf(digNum));
            }
        });
        // 取消点赞
        viewHolder.getView(R.id.liked_num).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unDigPost(model.getId());
                viewHolder.goneIfEmpty(R.id.liked_num);
                viewHolder.show(R.id.like_num);
                int digNum = model.getBoolLike() ? model.getLikeCount() - 1 : model.getLikeCount();
                // 开始喜欢 取消点赞 -1， 开始不喜欢 取消点赞不变
                viewHolder.setText(R.id.like_num, String.valueOf(digNum));

            }
        });
    }

    protected void addFriend(String friendId) {
        RemoteService service = Network.remote();
        Call<RspModel<Boolean>> call = service.friendRequestSend(friendId,null,null);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {
                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getContext());
                    return;
                }
                if(body !=null && body.success()){
                    BaseApplication.showToast("发送成功");
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {

            }
        });
    }


}
